package com.tx.studentManager.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tx.common.format.util.DateUtil;
import com.tx.studentManager.config.SystemConfig;
import com.tx.studentManager.model.Department;
import com.tx.studentManager.model.ErrorInfo;
import com.tx.studentManager.model.Major;
import com.tx.studentManager.model.Page;
import com.tx.studentManager.model.StudentClass;
import com.tx.studentManager.qo.StudentClassQO;
import com.tx.studentManager.service.DepartmentService;
import com.tx.studentManager.service.MajorService;
import com.tx.studentManager.service.StudentClassService;
import com.tx.studentManager.utils.DWZJsonUtil;

/**
 * 
 * @描述 班级管理控制器
 * @since studentManager-admin 1.0
 * @version 1.0 2015-9-14 上午9:40:51
 * @author 此生唯晴
 */
@Controller
@RequestMapping(value = "/clazz")
public class ClassController {
	private Logger logger=Logger.getLogger(ClassController.class);
	
	@Autowired
	private StudentClassService classService;
	@Autowired
	private MajorService majorService;
	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private SystemConfig systemConfig;

	/**
	 * 
	 * @描述 班级列表
	 * @return 班级管理视图
	 */
	@RequestMapping("/list")
	public ModelAndView list(
			HttpServletRequest request,
			@ModelAttribute StudentClassQO qo,
			@RequestParam(required = false) String createDateStartStr,
			@RequestParam(required = false) String createDateEndStr,
			@RequestParam(required = false, value = "numPerPage") Integer pageSize,
			@RequestParam(required = false, value = "pageNum") Integer pageNo) {
		if (StringUtils.isNotBlank(createDateEndStr)) {
			qo.setCreateDateEnd(DateUtil.str2DateTime(createDateEndStr));
		}
		if (StringUtils.isNotBlank(createDateStartStr)) {
			qo.setCreateDateStart(DateUtil.str2DateTime(createDateStartStr));
		}
		
		pageSize = pageSize == null ? systemConfig.getQueryPageSize() : pageSize;
		pageNo = pageNo == null ? 1 : pageNo;
		
		Page page = new Page();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		page.setCondition(qo);
		page = classService.list(page);
		List<Major> majors = majorService.findAll();
		List<Department> depList = departmentService.findAll();
		ModelAndView modelAndView = new ModelAndView("class/classList");
		modelAndView.addObject("page", page);
		modelAndView.addObject("majors", majors);
		modelAndView.addObject("depList",depList);
		return modelAndView;
	}

	/**
	 * 
	 * 添加班级页面，弹出窗口
	 * 
	 * @return
	 */
	@RequestMapping(value = "/add")
	public ModelAndView add() {
		List<Major> majors = majorService.findAll();
		ModelAndView modelAndView=new ModelAndView("class/classAdd");
		modelAndView.addObject("majors",majors);
		return modelAndView;
	}

	/**
	 * 
	 * @描述 添加班级操作
	 * @return 返回json
	 */
	@ResponseBody
	@RequestMapping(value = "/toAdd", produces = "text/html;charset=UTF-8")
	public String toAdd(@RequestParam(required=true)String className,@RequestParam(required=true)String majorId) {
		int classSize=classService.findBy("className", className).size();
		if (classSize>0) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE,
					"添加班级已存在！", DWZJsonUtil.OPER_CLOSE_CURRENT, "");
		}
		Major major=majorService.findById(majorId);
		if (major==null) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE,
					"所属专业不存在，请查正后再试！", null, "");
		}
		StudentClass studentClass=new StudentClass();
		studentClass.setMajor(major);
		studentClass.setClassName(className);
		StudentClass resultClass=classService.insert(studentClass);
		if (resultClass==null) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE,
					"添加失败！", "", "");
		}
		return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_SUCCESS_CODE,
				"添加成功！", DWZJsonUtil.OPER_CLOSE_CURRENT, "classManager");
	}

	/**
	 * 
	 * @描述 修改班级
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/modify")
	public ModelAndView modify(@RequestParam(required=true)String classId) {
		StudentClass studentClass=classService.findById(classId);
		ModelAndView modelAndView=new ModelAndView("class/classModify");
		if (studentClass==null) {
			modelAndView.setViewName("common/error");
			ErrorInfo errorInfo=new ErrorInfo();
			errorInfo.setErrorCode("500");
			errorInfo.setErrorMsg("所选班级不存在！");
			return modelAndView;
		}
		List<Major>majorList=majorService.findAll();
		modelAndView.addObject("majorList",majorList);
		modelAndView.addObject(studentClass);
		return modelAndView;
	}

	/**
	 * 
	 * 执行修改
	 * 
	 * @param clazz
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/toModify", produces = "text/html;charset=UTF-8")
	public String toModify(StudentClass clazz) {
		return "";
	}

	/**
	 * 
	 * @描述 删除班级
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delClass",produces = "text/html;charset=UTF-8")
	public String del(String classId) {
		StudentClass studentClass=classService.load(classId);
		if (studentClass==null) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE,
					"操作失败,班级不存在", "", "");
		}
		boolean opResult = false;
		try {
			opResult = classService.delete(classId);
		} catch (DataIntegrityViolationException e) {
			//e.printStackTrace();
			String errorMsg="存在关联学生，不能删除该班级！";
			logger.info(errorMsg);
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE,
					errorMsg, "", "");
		}
		if (opResult) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_SUCCESS_CODE,
					"删除成功！", "", "");
		}else
		return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE,
				"操作失败", "", "");
	}
}
